package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.VideoFocus;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

@DubboService
public class VideoFocusApiImpl implements VideoFocusApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 视频关注
     *
     * @param focus
     */
    @Override
    public void focus(VideoFocus focus) {
        mongoTemplate.save(focus);
    }


    /**
     * 视频取消关注
     * @param userId
     * @param fansId
     */
    @Override
    public void unFocus(Long userId, Long fansId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("fansId").is(fansId));
        mongoTemplate.remove(query, VideoFocus.class);
    }

    /**
     * 是否已关注
     * @param userId
     * @param fansId
     * @return
     */
    @Override
    public Boolean hasFocus(Long userId, Long fansId) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("fansId").is(fansId);
        Query query = Query.query(criteria);
        boolean flag = mongoTemplate.exists(query, VideoFocus.class);
        return flag;
    }
}
